package cn.itcast.nems.common.account;

import cn.itcast.ic.common.exception.v2.BizExceptionProducer;
import cn.itcast.nems.common.enumeration.NemsErrorBodyEnum;
import cn.itcast.nems.employee.dao.bean.EmployeeGroupBean;
import cn.itcast.nems.employee.dao.bean.EmployeeGroupsBean;

import java.util.List;
import java.util.Optional;

/**
 * author: yong.li
 * create: 2023-08-15 11:25:10
 **/
public class AccountHolder {
    private static final ThreadLocal<String> accountHolder = new ThreadLocal<>();
    private static final ThreadLocal<AccountInfo> accountInfoholder = new ThreadLocal<>();
    private static final ThreadLocal<EmployeeGroupsBean> accountEmployeeGroupsHolder = new ThreadLocal<>();

    /**
     * 当前账号设置为scheduler(定时任务)
     */
    public static void setSchedulerAccountInfo() {
        setAccount("scheduler");
        setAccountInfo(new AccountInfo(
                "scheduler",
                "定时任务",
                "000000",
                "信息中心"
        ));
    }

    /**
     * @param account 员工email
     * @deprecated 用setAccountInfo替代
     */
    public static void setAccount(String account) {
        accountHolder.set(account);
    }

    /**
     * @return 员工email
     * @deprecated 用getAccountInfo替代
     */
    public static Optional<String> getAccount() {
        return Optional.ofNullable(accountHolder.get());
    }

    /**
     * @return 员工email
     * @deprecated 用getRequiredAccountInfo替代
     */
    public static String getRequiredAccount() {
        return getAccount().orElseThrow(BizExceptionProducer.produce(NemsErrorBodyEnum.UNAUTHORIZED));
    }

    public static void setAccountInfo(AccountInfo accountInfo) {
        setAccount(accountInfo.getEmail());
        accountInfoholder.set(accountInfo);
    }

    public static Optional<AccountInfo> getAccountInfo() {
        return Optional.ofNullable(accountInfoholder.get());
    }

    public static AccountInfo getRequiredAccountInfo() {
        return getAccountInfo().orElseThrow(BizExceptionProducer.produce(NemsErrorBodyEnum.UNAUTHORIZED));
    }

    public static void setAccountEmployeeGroups(EmployeeGroupsBean employeeGroups) {
        accountEmployeeGroupsHolder.set(employeeGroups);
    }

    public static Optional<EmployeeGroupsBean> getAccountEmployeeGroups() {
        return Optional.ofNullable(accountEmployeeGroupsHolder.get());
    }

    public static EmployeeGroupsBean getRequiredAccountEmployeeGroups() {
        return getAccountEmployeeGroups().orElseThrow(BizExceptionProducer.produce(NemsErrorBodyEnum.UNAUTHORIZED));
    }

    /**
     * 判断当前登录账号是否有用户组权限；如果holder中没有员工组信息（定时任务、消息、oapi）表示有所有数据权限
     *
     * @return 是否有用户组权限
     */
    public static boolean hasEmployeeGroups() {
        final EmployeeGroupsBean employeeGroupsBean = accountEmployeeGroupsHolder.get();
        if (employeeGroupsBean != null) {
            return employeeGroupsBean.hasEmployeeGroups();
        }
        return true;
    }

    /**
     * 查询当前登录账号是否拥有指定校区及学科的数据权限
     *
     * @param campusId  校区code
     * @param subjectId 学科code
     * @return 是否有权限
     */
    public static boolean hasEmployeeGroups(String campusId, String subjectId) {
        final EmployeeGroupsBean employeeGroupsBean = accountEmployeeGroupsHolder.get();
        if (employeeGroupsBean != null) {
            return employeeGroupsBean.hasEmployeeGroups(campusId, subjectId);
        }
        return false;
    }

    /**
     * 获取当前登录账号的用户组
     *
     * @return 用户组，如果没有找到当前登录用户则返回null
     */
    public static List<EmployeeGroupBean> getEmployeeGroups() {
        final EmployeeGroupsBean employeeGroupsBean = accountEmployeeGroupsHolder.get();
        return employeeGroupsBean == null ? null : employeeGroupsBean.getGroupList();
    }

    public static void clear() {
        accountHolder.remove();
        accountInfoholder.remove();
        accountEmployeeGroupsHolder.remove();
    }
}
